/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <devmon/driver_abi.h>
#include <mc/log.h>

#include "hi183x_card.h"
#include "hi183x_om.h"
#include "hi183x_port.h"

#ifndef DEVICE_DRIVER_NAME
#define DEVICE_DRIVER_NAME "hisi_hi183x"
#endif

using namespace dev;

// 导出信息实现
extern "C" {

driver_handle_t create_hi183x_card(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            return nullptr;
        }
        hi183x_card* device = new hi183x_card();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating hi183x PCIeNicCard: ${error}", ("error", e.what()));
        return nullptr;
    }
}
status_t init_hi183x_card(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing hi183x PCIeNicCard: device is null");
        return STATUS_ERROR;
    }
    hi183x_card*      device_object  = static_cast<hi183x_card*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_hi183x_card(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting hi183x PCIeNicCard: device is null");
        return STATUS_ERROR;
    }
    hi183x_card* device_object = static_cast<hi183x_card*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_hi183x_card(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping hi183x PCIeNicCard: device is null");
        return STATUS_ERROR;
    }
    hi183x_card* device_object = static_cast<hi183x_card*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_hi183x_port(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            elog("Error creating hi183x NetworkPort: service or name is null");
            return nullptr;
        }
        hi183x_port* device = new hi183x_port();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating hi183x NetworkPort: ${error}", ("error", e.what()));
        return nullptr;
    }
}
status_t init_hi183x_port(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing hi183x NetworkPort: device is null");
        return STATUS_ERROR;
    }
    hi183x_port*      device_object  = static_cast<hi183x_port*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }

    return STATUS_OK;
}

status_t start_hi183x_port(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting hi183x NetworkPort: device is null");
        return STATUS_ERROR;
    }
    hi183x_port* device_object = static_cast<hi183x_port*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_hi183x_port(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping hi183x NetworkPort: device is null");
        return STATUS_ERROR;
    }
    hi183x_port* device_object = static_cast<hi183x_port*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_hi183x_optical_module(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            elog("Error creating hi183x OpticalModule: service or name is null");
            return nullptr;
        }

        hi183x_optical_module* device = new hi183x_optical_module();
        device->set_object_name(name);
        device->set_service(static_cast<mc::engine::service*>(service));
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating hi183x OpticalModule: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_hi183x_optical_module(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing hi183x OpticalModule: device is null");
        return STATUS_ERROR;
    }
    hi183x_optical_module* device_object  = static_cast<hi183x_optical_module*>(device);
    mc::mutable_dict*      csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*              connector_ptr  = static_cast<mc::dict*>(connector);
    bool                   ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }

    return STATUS_OK;
}

status_t start_hi183x_optical_module(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting hi183x OpticalModule: device is null");
        return STATUS_ERROR;
    }
    hi183x_optical_module* device_object = static_cast<hi183x_optical_module*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_hi183x_optical_module(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping hi183x OpticalModule: device is null");
        return STATUS_ERROR;
    }
    hi183x_optical_module* device_object = static_cast<hi183x_optical_module*>(device);
    device_object->stop();
    return STATUS_OK;
}

// pcie_nic_card设备类信息
device_driver_t hi183x_card_device_driver = {.device_name = "PCIeNicCard",
                                             .ctor        = create_hi183x_card,
                                             .init        = init_hi183x_card,
                                             .start       = start_hi183x_card,
                                             .stop        = stop_hi183x_card};
// network_port设备类信息
device_driver_t hi183x_port_device_driver = {.device_name = "NetworkPort",
                                             .ctor        = create_hi183x_port,
                                             .init        = init_hi183x_port,
                                             .start       = start_hi183x_port,
                                             .stop        = stop_hi183x_port};
// optical_module设备类信息
device_driver_t hi183x_optical_module_device_driver = {.device_name = "OpticalModule",
                                                       .ctor        = create_hi183x_optical_module,
                                                       .init        = init_hi183x_optical_module,
                                                       .start       = start_hi183x_optical_module,
                                                       .stop        = stop_hi183x_optical_module};

// 导出设备类信息
device_driver_t hi183x_device_driver[] = {hi183x_card_device_driver, hi183x_port_device_driver,
                                          hi183x_optical_module_device_driver};
status_t        register_device_driver(device_driver_t** device_driver, uint8_t* count) {
    *device_driver = hi183x_device_driver;
    *count         = sizeof(hi183x_device_driver) / sizeof(hi183x_device_driver[0]);
    return STATUS_OK;
}
}